//
//  SignatureView.swift
//  ezyform
//
//  Created by MoennigQ on 19/12/2016.
//  Copyright © 2016 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit

class SignatureView: UIView {

    // MARK: - If signatured
    internal var signaturedFlag = false
    // MARK: - Public properties
    internal var lineWidth: CGFloat = 6.0 {
        didSet {
            self.path.lineWidth = lineWidth
        }
    }
    internal var strokeColor: UIColor = UIColor.black
    internal var signatureBackgroundColor: UIColor = UIColor(red: 0.97, green: 0.97, blue: 0.97, alpha: 1)//UIColor.white
    
    // MARK: - Private properties
    fileprivate var path = UIBezierPath()
    fileprivate var pts = [CGPoint](repeating: CGPoint(), count: 5)
    fileprivate var ctr = 0
    
    
    // MARK: - Init
    required internal init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.accessibilityIdentifier = "signatureViewID"
        self.backgroundColor = self.signatureBackgroundColor
        self.path.lineWidth = self.lineWidth
    }
    
    // MARK: - Draw
    override internal func draw(_ rect: CGRect) {
        self.strokeColor.setStroke()
        self.path.stroke()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.accessibilityIdentifier = "signatureViewID"
        self.backgroundColor = self.signatureBackgroundColor
        self.path.lineWidth = self.lineWidth
    }
    
    // MARK: - Touch handling functions
    override internal func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let firstTouch = touches.first{
            let touchPoint = firstTouch.location(in: self)
            self.ctr = 0
            self.pts[0] = touchPoint
        }
    }
    
    override internal func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let firstTouch = touches.first{
            let touchPoint = firstTouch.location(in: self)
            self.ctr += 1
            self.pts[self.ctr] = touchPoint
            if (self.ctr == 4) {
                self.pts[3] = CGPoint(x: (self.pts[2].x + self.pts[4].x)/2.0, y: (self.pts[2].y + self.pts[4].y)/2.0)
                self.path.move(to: self.pts[0])
                self.path.addCurve(to: self.pts[3], controlPoint1:self.pts[1], controlPoint2:self.pts[2])
                
                self.setNeedsDisplay()
                self.pts[0] = self.pts[3]
                self.pts[1] = self.pts[4]
                self.ctr = 1
                //path绘制才算签名，修复只点击下但是签名是空白的问题
                signaturedFlag = true
                
            }
            
            self.setNeedsDisplay()
        }
    }
    
    override internal func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if self.ctr == 0{
            let touchPoint = self.pts[0]
            self.path.move(to: CGPoint(x: touchPoint.x-1.0,y: touchPoint.y))
            self.path.addLine(to: CGPoint(x: touchPoint.x+1.0,y: touchPoint.y))
            self.setNeedsDisplay()
            //path绘制才算签名，修复只点击下但是签名是空白的问题
            signaturedFlag = true
        } else {
            self.ctr = 0
        }
    }
    
    // MARK: - Helpers
    
    // MARK: Clear the Signature View
    internal func clearSignature() {
        signaturedFlag = false
        self.path.removeAllPoints()
        self.setNeedsDisplay()
    }
    
    // MARK: Save the Signature as an UIImage
    internal func getSignature() ->UIImage {
        self.layer.borderWidth = 0//去掉签名的边框
        self.backgroundColor = .clear
        //
        let renderer = UIGraphicsImageRenderer(bounds: self.bounds)
        let signature = renderer.image{ context in
            self.layer.render(in: context.cgContext)
        }
        self.layer.borderWidth = 1//截图后再加上
        return signature
    }

}
